package com.baishancloud.log.common.util

import com.baishancloud.log.common.entity.IpInfoEntity
import com.baishancloud.log.format.java.util.IPLocator

/**
 *
 * @author ziqiang.wang 
 * @date 2022/2/15 14:55
 */
object IpInfoUtil extends Serializable {

  /**
   * 通过ip地址获取其他信息
   *
   * @param ip ip地址
   * @return [[IpInfoEntity]]对象
   */
  def core(ip: String): IpInfoEntity = {
    //ip信息
    val ipInfo = IPLocator.lookup(ip)
    var isp: String = ipInfo.getIsp.split("/")(0)
    if (isp.equals("other")) isp = "其他"
    if (isp.contains("广电")) {
      isp = "广电"
    } else if (isp.contains("华数")) {
      isp = "华数"
    }
    val country: String = StringUtil.subString(if (ipInfo.getCountry.equals("other")) "其他" else ipInfo.getCountry, 33)
    var province: String = StringUtil.subString(if (ipInfo.getProvince.equals("other")) "其他" else ipInfo.getProvince, 33)
    if ((country.equals("中国") || country.startsWith("CHINA")) && province.equals("其他")) province = "中国其他地区"
    IpInfoEntity(isp, country, province)
  }

}
